/**
 * Created by 000188 on 2016/6/28.
 */
/**
 * Created by 000188 on 2016/6/27.
 */
import React from 'react';
import Ajax from 'superagent';
var Content = require('content');
var verify = require('verify');
var Btn = require('btn');
var LuckDraw = require('luck-draw');
var Logo = require('logo');
let EndMsg = require('end-msg');
let loadImg = require('load-img');
let Msg = require('msg');
var EndAct = require('end-act');

var UserForm = React.createClass({
    render(){
        return <div className="mobile-form">
            <Logo />
            <div className="mobile-form-box">
                <label>
                    <span>东方万里行会员卡号/证件号/手机号</span>
                    <input ref="userID" style={{width:'90%'}} type="text"/>
                </label>
                <label>
                    <span>密码</span>
                    <input ref="passwd" style={{width:'90%'}} type="password"/>
                </label>
                <label>
                    <input ref="dynamic" type="text"/>
                    <img ref="dynamicImg" onClick={this.updateValidateCode}/>
                </label>
                <Btn width="100%" text="　　去试试手气　　　" click={this.toScene}/>
                <Btn width='100%' text=' 非万里行会员点击注册' click={this.toReg} />
            </div>
        </div>
    },
    toReg(){
        require(['config'],Config=>{
            document.location.href = Config.api.reg + document.location.href;
        })
    },
    toScene(){
        let userID = this.refs.userID;
        let passwd = this.refs.passwd;
        let dynamic = this.refs.dynamic;

        if (passwd.value.length < 6) {
            Msg.alert('密码输入不正确')
            return
        }
        if (userID.value.length < 6) {
            Msg.alert('用户名输入不正确')
            return
        }
        if (dynamic.value.length < 4) {
            Msg.alert('动态验证码必须为4位')
            return
        }
        require(['config'],Config=>{
            Ajax.post(Config.api.user)
                .set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
                .send({
                type:3,
                username: userID.value,
                password: passwd.value,
                authcode: dynamic.value
            }).end((err, d)=> {
                if(err){
                    return;
                }
                let data = JSON.parse(d.text);
                if (data.SuccessCode == '0') {
                    switch (data.Code){
                        case '0':
                            Content.render(<LuckDraw />);
                            break;
                        case '1':
                            Content.render(<EndAct />);
                            break;
                    }
                }else if(data.SuccessCode == '-2'){
                    Content.render(<EndMsg imgUrl={loadImg.states[1]} />);
                }
                else{
                    Msg.alert(data.Message);
                }
            })
        })
    },
    updateValidateCode(){
        var dynamicImg = this.refs.dynamicImg;
        require(['config'], Config=> {
            dynamicImg.src = Config.api.ValidateCode + '?' + new Date().getTime();
        });
    },
    componentDidMount(){
        this.updateValidateCode();
    }
});

module.exports = UserForm;